<template>
  <div>
    <!-- 主页面table -->
    <el-table v-loading="loading" :data="skuList" border style="width: 100%">
      <el-table-column type="index" label="序号" align="center" width="80"></el-table-column>
      <el-table-column prop="skuName" label="名称" width="100"></el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width"></el-table-column>
      <el-table-column  label="默认图片" width="150">
        <template slot-scope="{row,$inex}">
        <img :src="row.skuDefaultImg" style="width:100px">
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="width"></el-table-column>
      <el-table-column prop="price" label="价格" width="width"></el-table-column>
      <el-table-column  label="操作" width="width">
       <template slot-scope="{row,$inex}">
        <el-button icon="el-icon-top" v-if="row.isSale==0" size="mini" title='上架' @click="upshelf(row)"></el-button>
        <el-button type="success"  v-if="row.isSale==1"  icon="el-icon-bottom" title='下架' size="mini" @click="downshelf(row)"></el-button>
        <el-button type="primary" icon="el-icon-edit" size="mini" title='编辑'></el-button>
        <el-button type="info" icon="el-icon-warning" size="mini" title='查看' @click="Look(row)" ></el-button>
        <el-button type="warning" icon="el-icon-delete" size="mini" title='删除' @click="removeSku(row)"></el-button>
       </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="text-align:center"
      @size-change="handleSizeChange"
      @current-change="getSkuList"
      :current-page="page"
      
      :page-sizes="[10, 20, 50]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total">
    </el-pagination>
    <!-- 抽屉 -->
    <el-drawer  :visible.sync="dialog" direction="rtl" custom-class="demo-drawer" >
      <div class="demo-drawer__content">
        <el-form label-width="100px">
          <el-form-item label="名称" >{{skuLists.skuName}}</el-form-item>
          <el-form-item label="描述" >{{skuLists.skuDesc}}</el-form-item>
          <el-form-item label="价格" >{{skuLists.price}}</el-form-item>
          <el-form-item label="平台属性" >
                  <el-tag type="success" v-for="(attr,index) in skuLists.skuAttrValueList" :key='attr.id' style='margin-left:10px'>{{attr.attrName}}</el-tag>
              </el-table-column>
            </el-table>
          </el-form-item>
          <el-form-item label="商品图片" >
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item v-for="item in skuLists.skuImageList" :key="item.id">
              <img :src="item.imgUrl" style='width:100%;height:100%'/>
              </el-carousel-item>
            </el-carousel>
          </el-form-item>
        </el-form>
      </div>
  </el-drawer>
  </div>
</template>

<script>
export default {
  name:'Sku',
  data(){
    return {
      skuList:[],
      page:1,
      limit:3,
      total:5,
      skuLists:{},
      // 加载布尔值
      loading:false,
      // 控制抽屉
      dialog:false,
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods:{
    async getSkuList(p=1){
      this.page=p
      this.loading=true
      let result=await this.$API.sku.getList(this.page,this.limit)
      if(result.code==200){
        this.skuList=result.data.records
        this.page=result.data.current
        this.total=result.data.total
        this.limit=result.data.size
        this.loading=false

      }
    },
    handleSizeChange(limit){
      this.limit=limit
      this.getSkuList()
    },
    //点击查看详情
    async Look(row){
      this.dialog=true
      // 根据SKU的id查询SKU的详细信息
      let result =await this.$API.sku.get(row.id)
      // // 根据指定的SPU的id查询所有对应的SKU的列表
      // let promise2=this.$API.sku.getListBySpuId(row.spuId)
      // // 获取指定SPU的id对应的图片列表
      // let promise3=this.$API.sku.getSpuImageList(row.spuId)
      // //  获取指定SPU的id对应的销售属性列表
      // let promise3=this.$API.sku.getSpuSaleAttrList(row.spuId)
      if(result.code==200){
          this.skuLists=result.data
      }
    },
    // 上架
    async upshelf(row){
      row.isSale=1
      let result=await this.$API.sku.onSale(row.id)
      if(result.code==200){
        this.$message({
          type: "success",
          message: "上架成功!",
        });
        this.getSkuList(this.page);
      }
    },
    // 下架
    async downshelf(row){
       row.isSale=0
      let result=await this.$API.sku.cancelSale(row.id)
      if(result.code==200){
        this.$message({
          type: "success",
          message: "下架成功!",
        });
        this.getSkuList(this.page);
      }
    },
    // 删除sku信息
    async removeSku(row){
      try{
        await this.$API.sku.remove(row.id)
        this.$message('删除成功')
        this.getSkuList(1)
      }catch(e){
        this.$message('删除失败')
      }
    }
  }
}
</script>

<style>
  .el-carousel__item h3 {
    color: #475669;
    font-size: 14px;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
  }
  
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>